{% extends "admin/admin_layout.html" %}
{% block admin_title %}管理角色用户列表{% endblock %}
{% block admin_head %}
<script type="text/javascript" src="/static/js/JsUser.js"></script>
{% endblock %}
{% block admin_content %}
{% set perm = request.perm_const %}
<div class="sect">
  <h2>“{{ this_role.name }}”角色 用户列表
    <span class="sub">共{{ page.paginator.count }}个用户</span>
    <span class="extra"> 
    	{% if perm.check_perm_op(perm.perm_role,perm.op_change) %}
    		<a href="javascript:;" class="act_add" onclick="show_add_role_user(event);">添加用户</a> 
    	{% endif %}
    </span>
  </h2>
  <div class="sectContent">
    <table id="role_user_table" class="gTable">
      <tr>
        <th class="m1"></th>
        <th class="m4">ID</th>
        <th class="m0">用户列表</th>
      </tr>
      {% for obj in page.object_list %}
      <tr>
        <td class="m1"><input type="checkbox" autocomplete="off" value="{{ obj.id }}"  id="chk_{{ obj.id }}" /></td>
        <td class="m4">{{ obj.id }}</td>
        <td><a href="/user/{{ obj.id }}/">{{ obj.nick_name }}</a>
            (<span class="hint">{{ "%s  %s"|format(obj.name,request.utils.getGenderArea(obj,False)) }}</span>)
        </td>
      </tr>
      {% endfor %}
      <tr>
      	<td><input type="checkbox" autocomplete="off" id="chkAll" onclick="$checkAll('role_user_table','chkAll');" /></td>
        <td colspan="2">
        	{% if perm.check_perm_op(perm.perm_role,perm.op_change) %}
          	<input type="button" class="button btns" value="删除选中" onclick="del_role_user('role_user_table','chkAll','{{ this_role.id }}');" />
          	{% endif %}
        </td>
      </tr>
    </table>
    {% include "paginator.html" %}
   </div>
</div>
{% endblock %}
{% block popup_content %}
<div id="s_role_user_div" class="popup popmiddle" style="display:none;">
  <div class="sect">
    <h2>
      <span id="s_role_user_title" class="title">添加用户</span>
      <span class="extra" onclick="$('s_role_user_div').tHide();">关闭</span>
    </h2>
    <div class="sectContent gForm">
      <div class="row">
        <label>用户昵称包含：</label>
        <div class="col"> 
	        <input type="text" class="inputbox middleinput" id="s_nick" name="s_nick" />
	        <input type="hidden" id="s_id" name="s_id" />
	        <a href="javascript:;" onclick="find_user('s_nick','s_user_table','s_nick','s_id',1);">查找</a> 
	        <a href="javascript:;" onclick="$('s_user_table').innerHTML = '';">清除</a> 
	        <div id="s_user_table"> </div>
        </div>
      </div>
      <div class="sectBtn">
        <input type="button" class="button" value="确定" onclick="add_role_user('s_user_table','{{ this_role.id }}');" />
        <a href="javascript:;" onclick="$('s_role_user_div').tHide();">取消</a> </div>
    </div>
  </div>
</div>
<script type="text/javascript">
function show_add_role_user(ev){
    $("s_nick").value = "";$("s_user_table").innerHTML = "";$('s_role_user_div').fshow({event:ev});
}
function add_role_user(parent, role_id){
    var ptable = $(parent);
    var usr_ids = $("s_id").value;
    if (!usr_ids) {new MsgBox({message:"没有选中任何用户"});return;}
    var params = "role_id=" + role_id + "&value=" + usr_ids;
    new echoAjax({url:"/admin/role/user/add/", params:params,reLoad:true}).fire();
}
function del_role_user(parent, chkall, role_id){
    if (!confirm("你确定要删除选中的内容吗？"))return;
    var ids = $getCheckValues(parent, chkall);
    if (!ids) {new MsgBox({message:"没有选中任何内容"});return;}
    var params = "role_id=" + role_id + "&value=" + ids;
    new echoAjax({url:"/admin/role/user/delete/", params:params,reLoad:true}).fire();
}
/* add_box=0, add nothing, =1, add checkbox, =2, add radio box*/
function find_user(nick_inner, rsdiv_id, txt_id, value_id, box_type){
    var nick = $(nick_inner).value;
    if (nick == null || nick == "") {
        new MsgBox({message:"您还没有输入查询条件!"});
        return;
    }
    var result_div = $(rsdiv_id);
    result_div.innerHTML = "正在检索...";
    var params = "nick=" + String.encode(nick);
    var ajax = new Request.JSON({
        url: "/user/find/",
        data: params,
        onSuccess: function(response){
            // {result:false/true, users:[{id:$id, nick_name: $nick_name,gender:$gender,area:area},{}...]}
            if (response.result == "true" || response.result == true) {
                var usrs = response.message;
				var objs = new Array();
				for(var i=0;i<usrs.length;i++){
					var usr = usrs[i];
					var obj = new Object();
					obj.value = usr.id;
					obj.name = usr.nick;
					obj.desc = String.format("{0} | {1} {2}",usr.name,usr.gender,usr.area);
					obj.url = "/user/" + usr.id + "/";
					objs[i] = obj;
				}
				new List({ objects: objs, htmlContainer:rsdiv_id, textContainer:txt_id,
							valueContainer:value_id, checkType:'checkbox'
						});
            }
            else {
                result_div.innerHTML = response.message;
            }
        }
    }).send();
}

</script>
{% endblock %}